#include "objectwindow.h"
#include "ui_objectwindow.h"

ObjectWindow::ObjectWindow(DEFINE_LIST defineList, OBJECT_LIST objectList, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ObjectWindow),
    m_defineList(defineList),
    m_objectList(objectList)
{
    ui->setupUi(this);
    //初始化视图
    initView();
}

ObjectWindow::~ObjectWindow()
{
    delete ui;
    delete m_tableModel;
    delete m_treeModel;
}

void ObjectWindow::initView()
{
    //设置主窗口标题
    setWindowTitle("检测结果");
    //重置窗口大小
    resize(1200, 800);
    //设置最大化显示
    //showMaximized();
    //更新常量数量信息
    ui->defineGroup->setTitle(QString("常量（%1）").arg(m_defineList.size()));
    //更新对象数量信息
    ui->objectGroup->setTitle(QString("对象（%1）").arg(m_objectList.size()));
    //初始化表格
    m_tableModel = new QStandardItemModel(ui->tableView);
    QStringList tableHeaderLabels = QStringList()<<"名称"<<"值";
    m_tableModel->setHorizontalHeaderLabels(tableHeaderLabels);
    ui->tableView->setModel(m_tableModel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //显示表格
    int defineIndex = 0;
    Q_FOREACH(QString defineName, m_defineList.keys()){
        QString defineValue = m_defineList[defineName];
        QStandardItem* defineNameNode = new QStandardItem(defineName);
        QStandardItem* defineValueNode = new QStandardItem(defineValue);
        defineNameNode->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        defineValueNode->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        m_tableModel->setItem(defineIndex, 0, defineNameNode);
        m_tableModel->setItem(defineIndex, 1, defineValueNode);
        defineIndex++;
    }
    //初始化树结构
    m_treeModel = new QStandardItemModel(ui->treeView);
    ui->treeView->setModel(m_treeModel);
    ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QStringList treeHeaderLabels = QStringList()<<"属性"<<"值";
    m_treeModel->setHorizontalHeaderLabels(treeHeaderLabels);
    ui->treeView->header()->setSectionResizeMode(QHeaderView::Stretch);
    ui->treeView->header()->setDefaultAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    //显示树结构
    int objectIndex = 0;
    Q_FOREACH(QString objectName, m_objectList.keys()){
        QList<QStandardItem*> objectNodeRow;
        QStandardItem* objectNode = new QStandardItem(objectName);
        objectNodeRow.append(objectNode);
        m_treeModel->appendRow(objectNodeRow);
        int objectElementIndex = 0;
        Q_FOREACH(QString objectElementName, m_objectList[objectName].keys()){
            QList<QStandardItem*> objectElementNodeRow;
            QStandardItem* objectElementNode = new QStandardItem(objectElementName);
            objectElementNodeRow.append(objectElementNode);
            objectNode->appendRow(objectElementNodeRow);
            int objectElementParamIndex = 0;
            Q_FOREACH(QString objectElementParamName, m_objectList[objectName][objectElementName].keys()){
                QString objectElementParamValue = m_objectList[objectName][objectElementName][objectElementParamName];
                QList<QStandardItem*> objectElementParamNodeRow;
                QStandardItem* objectElementParamNameNode = new QStandardItem(objectElementParamName);
                QStandardItem* objectElementParamValueNode = new QStandardItem(objectElementParamValue);
                objectElementParamNodeRow.append(objectElementParamNameNode);
                objectElementParamNodeRow.append(objectElementParamValueNode);
                objectElementNode->appendRow(objectElementParamNodeRow);
                objectElementParamIndex++;
            }
            objectElementIndex++;
        }
        objectIndex++;
    }
    ui->treeView->expandAll();
}
